Seriously Good Software: Code that works, survives, and wins by Marco Faella

Seriously Good Software: Code that works, survives, and wins by Marco Faella

Author:Marco Faella
Language: eng
Format: mobi, epub, pdf
Publisher: Manning Publications
Published: 2020-03-16T10:06:57.356000+00:00


1 The original procedure goes here (modifies u and v).

int gcd = -u * ( 1 <{}< k);

assert isGcd(gcd, originalU, originalV) : "Wrong GCD!";

return gcd;

}

For the auxiliary isGcd method, I said the simplest solution is preferable. In this case, you may simply apply the definition of “greatest common divisor,” and check that

gcd is indeed a common divisor of originalU and originalV

every larger number is not a common divisor

private static boolean isGcd(int gcd, int u, int v) {

if (u \% gcd != 0 || v \% gcd != 0) 1 Checks that gcd is a common divisor

return false;

for (int i=gcd+1; i<=u && i<=v; i++) 2 Checks any larger number,

if (u \% i == 0 && v \% i == 0) up to the minimum of u and v

return false;

return true;

}

This implementation of isGcd is very inefficient, being linear in the size of the smallest number between u and v. A more reasonable course of action would be to use Euclid’s classic algorithm (https://en.wikipedia.org/wiki/Euclidean_algorithm) or invoke an already implemented GCD procedure, such as the BigInteger.gcd() method from the JDK.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.